package com.wiegand.loader {
	import com.wiegand.event.LoaderEvent;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.net.URLRequest;

	/**
	 * @author kwiegand
	 */
	 
	public class SoundLoader extends EventDispatcher{

		private static var _instance : SoundLoader;

		private var _files : Array;										
		private var _xmlList : XMLList;						
		private var _data : Array;		
		private var _i : int = 0;

		public static function getInstance() : SoundLoader{		
			if( _instance == null )
				_instance = new SoundLoader( new SingletonEnforecer() );
			return _instance;
		}
		
		public function SoundLoader( blocker:SingletonEnforecer ):void{
			if ( blocker == null ) {	
            	throw new Error("Error: Instantiation failed: Use SoundLoader.getInstance() instead of new.");        
          	}
          	else{     	
          		init(); 	
          	}
		}
		
		private function init() : void{
			_data = new Array();
			_files = new Array();
		}
		
		public function load( xml : XMLList ):void{
		
			try{
								
				_xmlList = xml;
		
				for( var i : int = 0; i < _xmlList.children().length(); i++ ){
					
					var name : String = ( _xmlList.children()[ i ] as XML ).attribute( "name" );
					var file : String = ( _xmlList.children()[ i ] as XML ).toString();
					
					_files.push( [ name, file ] );
					
				}
			
				dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_START_LOADING ) );	
				loadFile();
				
			}
			catch ( e : TypeError){
								
			}		
			
		}
		
		private function loadFile() : void{
		
			if( _i < _files.length ){
								
				var soundObject : Sound = new Sound();
				soundObject.load( new URLRequest( _files[ _i ][ 1 ] ) );
				soundObject.addEventListener( Event.COMPLETE, handleComplete );
				
			}
			else{
				
				dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_LOADING_COMPLETE ));	
				
			}
		
		}
		
		private function handleComplete( event : Event ) : void {
			
			( event.target as Sound ).removeEventListener( Event.COMPLETE, handleComplete );
			
			try{
								
				_data.push( ( event.target as Sound ) );
				_i++;
				loadFile();			
				
			}
			catch ( e : TypeError){
								
			}
			
		}
		
		public function getSoundData() : Array{
			
			return _data;
			
		}
		
		public function getSound( s : String ) : Sound{
			
			var sound : Sound;
			
			for( var i : int = 0; i < _files.length; i++ ){
			
				if( s == _files[ i ][ 0 ] ){
					sound = _data[ i ] as Sound;
				}
				
			}
			
			return sound;
			
		}
		
		public function clear():void{
			
			for( var i : int = 0; i < _files.length; i++){
				_files[i][0] = null;
				_files[i][1] = null;
				_files[i] = null;
			}
			_files = null;

			for( var j : int = 0; j < _data.length; j++){
				_data[j] = null;
			}
			
			_xmlList = null;
		
		}
	}
}

internal class SingletonEnforecer {}
